package com.xiaoq.users.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.xiaoq.coms.util.CommonUtil;
import com.xiaoq.coms.util.NetUtil;
import com.xiaoq.schedule.user.IUserScheduleApi;
import com.xiaoq.users.entity.UserPhoneEntity;
import com.xiaoq.users.repository.UserPhoneRepository;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.json.simple.JSONValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

@Service(interfaceClass=IUserScheduleApi.class,version="1.0.0")
public class UserScheduleImpl implements IUserScheduleApi {

	@Autowired
	private UserPhoneRepository userPhoneRepository;

	private static final String SHOUJI_URL = "http://v.showji.com/Locating/showji.com20180331.aspx";
	private Logger logger = LoggerFactory.getLogger(UserScheduleImpl.class);



	@Override
	public void updateUserPhoneBelongInfo() throws Exception{
		List<UserPhoneEntity> userPhoneList = userPhoneRepository.getUserPhoneList4ScheduleNeedUpdpate(20);
		if(CollectionUtils.isNotEmpty(userPhoneList)){
			logger.info("count*********************{}", userPhoneList.size());
			for(UserPhoneEntity userPhoneEntity : userPhoneList){
				if(CommonUtil.veriyMobile(userPhoneEntity.getMobile())){
					String url = SHOUJI_URL+"?m="+userPhoneEntity.getMobile()+"&output=json&callback=querycallback&timestamp="+System.currentTimeMillis();
					try {
						String result = NetUtil.readContentFromGet(url);
						if(result != null){
							result = StringUtils.substringBetween(result, "(", ")");
							//{"Mobile":"19913141443","QueryResult":"True","TO":"中国电信","Corp":"中国电信","Province":"北京","City":"北京","AreaCode":"010","PostCode":"100000","VNO":"","Card":""}
							if(result.contains("True")){
								Map<String, String> requestMap = (Map<String, String>)JSONValue.parse(result);
								UserPhoneEntity updateEntity = new UserPhoneEntity();
								updateEntity.setId(userPhoneEntity.getId());
								updateEntity.setMobile(userPhoneEntity.getMobile());
								updateEntity.setUserId(userPhoneEntity.getUserId());
								updateEntity.setPhoneValid(true);
								updateEntity.setCity(requestMap.get("City"));
								updateEntity.setAreaCode(requestMap.get("AreaCode"));
								updateEntity.setPostCode(requestMap.get("PostCode"));
								updateEntity.setProvince(requestMap.get("Province"));
								updateEntity.setCorp(requestMap.get("Corp"));
								userPhoneRepository.update(updateEntity);
							}else {
								UserPhoneEntity updateEntity = new UserPhoneEntity();
								updateEntity.setId(userPhoneEntity.getId());
								updateEntity.setUserId(userPhoneEntity.getUserId());
								updateEntity.setMobile(userPhoneEntity.getMobile());
								updateEntity.setPhoneValid(false);
								userPhoneRepository.update(updateEntity);
							}
						}
					}catch (Exception e){
						throw new Exception(e.getMessage());
					}
				}else {
					UserPhoneEntity updateEntity = new UserPhoneEntity();
					updateEntity.setId(userPhoneEntity.getId());
					updateEntity.setUserId(userPhoneEntity.getUserId());
					updateEntity.setMobile(userPhoneEntity.getMobile());
					updateEntity.setPhoneValid(false);
					userPhoneRepository.update(updateEntity);
				}
			}
		}
	}

}
